package com.zys.background.common.web.exception.handler;

import com.zys.background.common.entity.Result;
import com.zys.background.common.web.exception.BgException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;


/**
 * @author zys
 * @since 2022-11-12
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

  @ExceptionHandler(ConstraintViolationException.class)
  public Result<Void> constraintViolationException(ConstraintViolationException e) {
    log.error("", e);
    String message = e.getMessage();
    return Result.fail(message.substring(message.indexOf(":") + 1));
  }

  @ExceptionHandler(BgException.class)
  public Result<Void> bgException(BgException e) {
    log.error("", e);
    return Result.fail(e.getMessage());
  }

  @ExceptionHandler(Exception.class)
  public Result<Void> exception(Exception e) {
    log.error("", e);
    return Result.fail();
  }
}
